package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.User;
import com.itheima.reggie.mappers.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public User login(String phone, String code, String verifyCode) {
        //1.校验验证码，验证码错误则返回null
        if (!verifyCode.equalsIgnoreCase(code)) {
            return null;
        }

        //2.通过手机号查询用户，没有则创建用户
        User user = userMapper.findByPhone(phone);
        if (user == null) {
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userMapper.save(user);
        }

        return user;
    }
}
